package pers.karl.mvc.module.user.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import pers.karl.mvc.module.user.entity.User;
import pers.karl.mvc.module.user.service.UserService;
import pers.karl.mvc.common.util.ExcelWebUtil;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 用户Controller
 * @author karl
 */
@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 新增用户
     * @param user
     * @return
     */
    @ResponseBody
    @PostMapping("add")
    public User addUser(User user) {
        return userService.createUser(user);
    }

    @ResponseBody
    @RequestMapping("list")
    public List<User> list(){
        return userService.listUser();
    }

    @ResponseBody
    @RequestMapping("page")
    public IPage<Map<String, Object>> page(Long currentPage, Long pageSize) {
        return userService.pageUser(currentPage,pageSize);
    }

    @ResponseBody
    @RequestMapping("export")
    public ResponseEntity export() throws IOException {
        Workbook workbook = new HSSFWorkbook();
        Sheet sheet = workbook.createSheet();
        Row row = sheet.createRow(0);
        Cell cell = row.createCell(0);
        cell.setCellValue("测试Excel");

        return ExcelWebUtil.export(workbook,"测试Excel");
    }

}
